/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.Tenpay;
import com.boxin.ims.modules.wechat.service.TenpayService;

/**
 * 财付通支付Controller
 * @author Jakemanse
 * @version 2013-09-21
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/tenpay")
public class TenpayController extends BaseController {

	@Autowired
	private TenpayService tenpayService;
	
	@ModelAttribute
	public Tenpay get(@RequestParam(required=false) Long id) {
		if (id != null){
			return tenpayService.get(id);
		}else{
			return new Tenpay();
		}
	}
	
	@RequiresPermissions("wechat:tenpay:view")
	@RequestMapping(value = {"list", ""})
	public String list(Tenpay tenpay, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			tenpay.setUser(user);
		}
        Page<Tenpay> page = tenpayService.find(new Page<Tenpay>(request, response), tenpay); 
        model.addAttribute("page", page);
		return "modules/wechat/tenpayList";
	}

	@RequiresPermissions("wechat:tenpay:view")
	@RequestMapping(value = "form")
	public String form(Tenpay tenpay, Model model) {
		User user = UserUtils.getUser();
		if(tenpay.getId() == null){
			tenpay.setUser(user);
		}
		
		Tenpay temp = tenpayService.getByUserId(user.getId());
		if(temp != null){
			tenpay = temp;
		}
		
		model.addAttribute("tenpay", tenpay);
		return "modules/wechat/tenpayForm";
	}

	@RequiresPermissions("wechat:tenpay:edit")
	@RequestMapping(value = "save")
	public String save(Tenpay tenpay, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tenpay)){
			return form(tenpay, model);
		}
		tenpayService.save(tenpay);
		addMessage(redirectAttributes, "保存财付通成功");
		if(UserUtils.getUser().isAdmin()){
			return "redirect:"+Global.ADMIN_PATH+"/wechat/tenpay/?repage";
		}else{
			return "redirect:"+Global.ADMIN_PATH+"/wechat/tenpay/form";
		}
	}
	
	@RequiresPermissions("wechat:tenpay:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		tenpayService.delete(id);
		addMessage(redirectAttributes, "删除财付通成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/tenpay/?repage";
	}

}
